package LeetCode.题库.简单;

/**
 * @author 挚爱之夕
 * @date 2022-03-06 - 03 - 06 - 9:38
 * @Description 给你一个整数 x ，如果 x 是一个回文整数，返回 true，否则返回false
 * @Version 简单
 */
public class _9回文数 {
    int x = 121;
    /*by me*/
    //1.转字符串
    public boolean isPalindrome(int x) {
        //负数不是回文数
        if(x < 0)
            return false;
        String s = Integer.toString(x);
        int left = 0, right = s.length() - 1;
        while(left < right){
            if(s.charAt(left) != s.charAt(right))
                return false;
            left++;
            right--;
        }
        return true;
    }
    /*官方思路*/
    //2.取右边一半的翻转数字
    public boolean isPalindrome1(int x) {
        //负数情况
        if(x < 0)
            return false;
        //个位为0的情况
        if(x !=0 && x %10 == 0)
            return false;
        int reverseHalfNum = 0;

        //取右半边翻转数字
        while(x > reverseHalfNum){
            reverseHalfNum *= 10;
            reverseHalfNum += x % 10;
            x = x / 10;
        }

        //考虑奇偶情况
        return x == reverseHalfNum || reverseHalfNum / 10 == x;
    }
}
